package com.james.codeauto.util;

import com.james.codeauto.common.StringUtils;
import com.james.codeauto.tools.json.JSONArray;
import com.james.codeauto.tools.json.JSONObject;
import org.apache.poi.ooxml.POIXMLDocument;
import org.apache.poi.ooxml.extractor.POIXMLTextExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.*;

/**
 * @Author：hp
 * @Description：
 * @Date：2021年11月4日14:58:11
 * @Modified by：读取word所有内容
 **/
public class ParkingLotYuShiDocUtil {
    public static void main(String[] args) {
        try {
            parseHtmlData();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // String content = readWord(parseDocFromGreenCloudPath());
        // System.out.println(content);
    }

    public static List<Map<String, Object>> parseHtmlData() throws Exception {
        HashSet<String> paramKeyTypes = new HashSet<>();
        List<Map<String, Object>> listData = new ArrayList<>();
        FileInputStream fileOutputStream = new FileInputStream(parseHtmlFromGreenCloudPath());
        String htmlText = new String(fileOutputStream.readAllBytes());
        fileOutputStream.close();
        Document doc = Jsoup.parse(htmlText);
        Elements h2AllElement = doc.getElementsByTag("h2");
        Elements h2Element = new Elements();
        int firstIndex = -1;
        for (int i = 0; i <= h2AllElement.size() - 1; i++) {
            String text = h2AllElement.get(i).text();
            if (StringUtils.isNotBlank(text)) {
                if ("心跳保活".equals(text)) {
                    firstIndex = i;
                }
                if ("下行接口说明".equals(text) ||
                        "支持设备".equals(text) ||
                        "接入方式".equals(text) ||
                        "接口描述".equals(text) ||
                        "加密方式".equals(text) ||
                        "消息格式".equals(text) ||
                        "协议规范".equals(text) ||
                        "下行接口说明".equals(text)) {
                    continue;
                }
                if (firstIndex > 0) {
                    h2Element.add(h2AllElement.get(i));
                }
            }

        }
        h2Element.remove(h2Element.size() - 1);
        h2Element.remove(h2Element.size() - 1);
        h2Element.remove(h2Element.size() - 1);
        h2Element.remove(h2Element.size() - 1);
        System.out.println(h2Element.size());
        Elements childE = doc.getElementsByClass("content-right").first().children();
        Elements pNewAllElement = new Elements();
        int firstP = -1, secondEndP = -1, skipMidd = -1;
        for (int i = 0; i < childE.size(); i++) {

            if (childE.get(i).text().contains("响应参数 { \"msg\":\"成功\", \"cipher\":\"Mk")) {
                skipMidd = -1;
                continue;
            }

            if (secondEndP > 0) {
                continue;
            }
            if (skipMidd > 0) {
                continue;
            }

            if (!"p".equals(childE.get(i).nodeName())) {
                continue;
            }
            if (!StringUtils.isNotBlank(childE.get(i).text())) {
                continue;
            }
            if (childE.get(i).text().contains("cloud/keepalive")) {
                firstP = i;
            }
            if (childE.get(i).text().contains("本协议用以规范云端及")) {
                skipMidd = i;
                pNewAllElement.remove(pNewAllElement.size() - 1);
                continue;
            }


            if (firstP >= 0) {

                String p = childE.get(i).attr("class");
                pNewAllElement.add(childE.get(i));
            }

            if (childE.get(i).text().contains("{ \"Num\":2, \"BookingRuleList\"")) {
                secondEndP = i;
            }


        }


        pNewAllElement.remove(pNewAllElement.size() - 1);
        //按照path 分组，不够进行补齐
        List<List<Element>> subArrayList = new ArrayList<>();
        Elements pNewElement = new Elements();
        int indexFlag = -1;
        boolean firstTable = false;
        List<Element> subList = null;
        for (int i = 0; i < pNewAllElement.size(); i++) {
            Element element = pNewAllElement.get(i);
            if (element.text().contains("Path：")) {
                if (subList != null && subList.size() > 0) {
                    subArrayList.add(subList);
                }
                subList = new ArrayList<>();
            }
            subList.add(element);
        }
        subArrayList.add(subList);
        int a = 0, b = 0;
        for (int i = 0; i < subArrayList.size(); i++) {
            List<Element> subItemList = subArrayList.get(i);


            if (subItemList.size() < 6) {
                for (int j = 0; j < subItemList.size(); j++) {
                    if (subItemList.get(j).text().contains("接口描述")) {
                        if (j + 1 < subItemList.size()) {
                            if (subItemList.get(j + 1).text().contains("Headers")) {
                                subItemList.add(j + 1, new Element("interface_desc"));
                            } else {
                                subItemList.add(new Element("header"));
                                subItemList.add(new Element("body"));
                            }
                        } else {
                            //end
                            subItemList.add(new Element("interface_desc"));
                            subItemList.add(new Element("header"));
                            subItemList.add(new Element("body"));
                        }
                    }

                }

            }


            if (subItemList.size() > 6) {
                int findBegin = -1;
                String allText = "";
                for (int j = subItemList.size() - 1; j >= 0; j--) {
                    //合并
                    if (findBegin > 0) {
                        if (subItemList.get(j).text().contains("接口描述")) {
                            findBegin = -1;
                            subItemList.get(j + 1).text(allText);
                            continue;
                        }

                        allText = subItemList.get(j).text() + "\n" + allText;
                        if (!subItemList.get(j - 1).text().contains("接口描述")) {
                            subItemList.remove(j);
                        }

                        continue;
                    }
                    if (subItemList.get(j).text().contains("Headers")) {
                        findBegin = j;
                        continue;
                    }


                }


            }
        }

        for (int i = 0; i < subArrayList.size(); i++) {
            List<Element> subItemList = subArrayList.get(i);
            for (int j = 0; j < subItemList.size(); j++) {
                pNewElement.add(subItemList.get(j));
            }
        }


        System.out.println(a + "" + b);//10个一组
        int size = pNewElement.size() / 6;
        System.out.println(size + "");//6个一组
        Elements tbody0Element = new Elements();
        int ignoreEnd = -1;
        int totalFind = 0;
        for (int i = 0; i < childE.size(); i++) {
            if (ignoreEnd > 0) {
                continue;
            }
            if (i > 0) {
                if ("h2".equals(childE.get(i - 1).nodeName()) && childE.get(i - 1).text().equals("消息格式")) {
                    continue;
                }

            }

            if ("h1".equals(childE.get(i).nodeName()) && "数据字典".equals(childE.get(i).text())) {
                ignoreEnd = 1;
                continue;
            }

            if (childE.get(i).text().contains("大型汽车")) {
                ignoreEnd = 1;
                continue;
            }

            if ("h3".equals(childE.get(i).nodeName()) && "请求参数".equals(childE.get(i).text())) {
                if (childE.get(i).id().equals("-4") ||
                        childE.get(i).id().equals("-16")) {
                    continue;
                }
                if (childE.get(i - 1).text().contains(" \"ID\": \"1\",")) {
                    System.out.println("a");
                }
                totalFind++;
                //寻找最近下一个h2标题 之间是否存在 h3 返回数据
                boolean hasTesTable = false;
                int skip = 0;
                if (!childE.get(i + 1).text().trim().equals("Headers")) {

                    tbody0Element.add(new Element("table"));
                } else {
                    tbody0Element.add(childE.get(i + 2).getElementsByTag("tbody").first());
                    skip = i + 2;
                }
                if (i + 3 >= childE.size()) {
                    tbody0Element.add(new Element("table"));
                    tbody0Element.add(new Element("table"));
                    continue;
                }
                if (!childE.get(i + 3).text().trim().equals("Body")) {
                    //存在顺序倒置
                    tbody0Element.add(new Element("table"));

                } else {
                    tbody0Element.add(childE.get(i + 4).getElementsByTag("tbody").first());
                    skip = i + 4;
                }
                int m = 0;
                for (int j = i + 1; j < i + 8; j++) {
                    if ("h2".equals(childE.get(j).nodeName())) {
                        for (int k = i + 1; k < j; k++) {
                            if ("h3".equals(childE.get(k).nodeName())) {
                                if (childE.get(k).text().equals("返回数据")) {
                                    m = k + 1;
                                    hasTesTable = true;
                                    break;
                                }
                            }
                        }
                        break;
                    }
                }


                if (!hasTesTable) {
                    //存在顺序倒置
                    tbody0Element.add(new Element("table"));
                } else {
                    tbody0Element.add(childE.get(m).getElementsByTag("tbody").first());

                }

                if (m > 0) {
                    i = m + 1;
                    continue;
                }
                if (skip > 0) {
                    i = skip + 1;
                    continue;
                }
                continue;
            }
            if (!"table".equals(childE.get(i).nodeName())) {
                continue;
            }
            if (!firstTable) {
                firstTable = true;
                continue;
            }

            tbody0Element.add(childE.get(i).getElementsByTag("tbody").first());

        }
        System.out.println("==" + totalFind);
        //39 * 3 = 117
        System.out.println(tbody0Element.size());
        Elements tbodyElement = tbody0Element;


        int tbodyElementSize = tbodyElement.size() / 3;
        System.out.println(tbodyElementSize + "");//3个一组

        for (int i = 0; i < h2Element.size(); i++) {
            Map<String, Object> parseMap = new HashMap<>();
            String title = h2Element.get(i).text();
            parseMap.put("title", title.substring(title.indexOf("、") + 1).trim());
            parseMap.put("title", parseMap.get("title").toString().substring(parseMap.get("title").toString().lastIndexOf(".") + 1));
            String titleT = (String) parseMap.get("title");
            if (Character.isDigit(titleT.charAt(0))) {
                titleT = titleT.substring(1);
            }
            if (Character.isDigit(titleT.charAt(0))) {
                titleT = titleT.substring(1);
            }

            parseMap.put("title", titleT);

            //method ->  测试环境： https://acscloudtest.parking24.cn/open_platform/portal/outer/get_park_info
            int descIndex = i * 6 + 3, urlIndex = i * 6;
//
            if (descIndex >= pNewElement.size()) {
                continue;
            }
            parseMap.put("desc", pNewElement.get(descIndex).text().replace("简要描述：", "").trim());
            parseMap.put("url", pNewElement.get(urlIndex).text().replace("Path：", "").replace("测试环境： ", "").trim().replace("测试环境: ", "").trim().replace("http://192.168.xx.xx:port", ""));
            parseMap.put("method", pNewElement.get(i * 6 + 1).text().replace("Method：", "").trim());
            parseMap.put("methodType",parseMap.get("method").toString().toLowerCase());

            Elements table1Element = tbodyElement.get(3 * i + 1).getElementsByTag("tr");
            JSONArray paramInfo = new JSONArray();
            if (table1Element != null) {

                for (int j = 0; j < table1Element.size(); j++) {
                    Elements table1TdElement = table1Element.get(j).getElementsByTag("td");
                    JSONObject jsonObjectParam = new JSONObject();
                    for (int k = 0; k < table1TdElement.size(); k++) {
                        Element td = table1TdElement.get(k);
                        String paramStr = td.text();
                        if (k == 0) {
                            //参数名
                            String style = td.getElementsByTag("span").first().attr("style");
                            jsonObjectParam.put("treeNode", "0");
                            if (style.equals("padding-left: 0px")) {

                            } else if (style.equals("padding-left: 20px")) {
                                jsonObjectParam.put("treeNode", "1");
                            } else if (style.equals("padding-left: 40px")) {
                                jsonObjectParam.put("treeNode", "2");
                            } else if (style.equals("padding-left: 60px")) {
                                jsonObjectParam.put("treeNode", "3");
                            } else if (style.equals("padding-left: 80px")) {
                                jsonObjectParam.put("treeNode", "4");
                            }
                            jsonObjectParam.put("name", paramStr.replace("├─", "").trim());
                            jsonObjectParam.put("realName", paramStr.trim());
                        } else if (k == 2) {
                            //必选
                            if (paramStr.trim().equals("必须")) {
                                jsonObjectParam.put("require", "是");
                            } else {
                                jsonObjectParam.put("require", "否");
                            }


                        } else if (k == 1) {

                            paramKeyTypes.add(paramStr);
                            //默认值
                            jsonObjectParam.put("type", paramStr);
                        } else if (k == 3) {
                            //说明
                            jsonObjectParam.put("defaultValue", "" + paramStr);
                        } else if (k == 4) {
                            //说明
                            jsonObjectParam.put("desc", paramStr);
                        } else if (k == 5) {
                            //说明
                            jsonObjectParam.put("descMore", "" + paramStr);
                        }

                        System.out.println(paramStr);
                    }
                    if (jsonObjectParam.get("name").toString().contains("参数名")) {
                        continue;
                    }
                    if (jsonObjectParam.get("name").toString().contains("utc_timestamp")) {
                        continue;
                    }
                    if (jsonObjectParam.get("name").toString().equals("sign")) {
                        continue;
                    }

                    if (jsonObjectParam.get("name").toString().contains("名称")) {
                        continue;
                    }

                    String reuqire = (String) jsonObjectParam.get("require");
                    String type = (String) jsonObjectParam.get("type");
                    if ("是".equals(reuqire) || "否".equals(reuqire)) {

                    } else {
                        jsonObjectParam.put("type", reuqire);
                        jsonObjectParam.put("require", type);
                    }
                    paramInfo.put(jsonObjectParam);
                }
            }
            parseMap.put("paramList", paramInfo);
            Elements table2Element = tbodyElement.get(3 * i + 2).getElementsByTag("tr");

            JSONArray responseParamInfo = new JSONArray();
            if (table2Element != null) {
                for (int j = 0; j < table2Element.size(); j++) {
                    Elements table2TdElement = table2Element.get(j).getElementsByTag("td");

                    JSONObject jsonObjectParam = new JSONObject();
                    int len = table2TdElement.size();
                    for (int k = 0; k < len; k++) {
                        Element td = table2TdElement.get(k);

                        String responseParamStr = td.text();
                        System.out.println(responseParamStr);
                        if (k == 0) {
                            //参数名
                            String style = td.getElementsByTag("span").first().attr("style");
                            jsonObjectParam.put("treeNode", "0");
                            if (style.equals("padding-left: 0px")) {

                            } else if (style.equals("padding-left: 20px")) {
                                jsonObjectParam.put("treeNode", "1");
                            } else if (style.equals("padding-left: 40px")) {
                                jsonObjectParam.put("treeNode", "2");
                            } else if (style.equals("padding-left: 60px")) {
                                jsonObjectParam.put("treeNode", "3");
                            } else if (style.equals("padding-left: 80px")) {
                                jsonObjectParam.put("treeNode", "4");
                            }
                            jsonObjectParam.put("name", responseParamStr.replace("├─", "").trim());
                            jsonObjectParam.put("realName", responseParamStr.trim());
                        } else if (k == 2) {
                            //必选
                            if (responseParamStr.trim().equals("必须")) {
                                jsonObjectParam.put("require", "是");
                            } else {
                                jsonObjectParam.put("require", "否");
                            }

                        } else if (k == 1) {

                            paramKeyTypes.add(responseParamStr);
                            //默认值
                            jsonObjectParam.put("type", responseParamStr.trim());
                        } else if (k == 3) {
                            //说明
                            jsonObjectParam.put("defaultValue", "" + responseParamStr);
                        } else if (k == 4) {
                            //说明
                            jsonObjectParam.put("desc", responseParamStr);
                        } else if (k == 5) {
                            //说明
                            jsonObjectParam.put("descMore", "" + responseParamStr);
                        }
                        System.out.println(responseParamStr);
                    }
                    if (jsonObjectParam.get("name").toString().contains("参数名")) {
                        continue;
                    }
                    if (jsonObjectParam.get("name").toString().contains("名称")) {
                        continue;
                    }
                    jsonObjectParam.put("treeNode", "0");
                    String reuqire = (String) jsonObjectParam.get("require");
                    String type = (String) jsonObjectParam.get("type");
                    if ("是".equals(reuqire) || "否".equals(reuqire)) {

                    } else {
                        jsonObjectParam.put("type", reuqire);
                        jsonObjectParam.put("require", type);
                    }
                    responseParamInfo.put(jsonObjectParam);

                }
            }
            parseMap.put("responseParamList", responseParamInfo);

            //构造json
            JSONObject jsonParamObject = convertListParamToJsonObj(true, paramInfo, parseMap);
            parseMap.put("paramJson", jsonParamObject);
            parseMap.put("paramJsonStr", jsonParamObject.toString(4));

            JSONObject responseJsonParamObject = convertListParamToJsonObj(false, responseParamInfo, parseMap);
            parseMap.put("responseJson", responseJsonParamObject);
            parseMap.put("responseParamJsonStr", responseJsonParamObject.toString(4));

            JSONObject response = (JSONObject) parseMap.get("responseJson");
            if (response.opt("Data") != null) {
                if (StringUtils.isNotBlank(response.optString("page_size"))) {
                    parseMap.put("hasPage", "true");
                }
                if (response.get("Data") instanceof JSONArray) {
                    parseMap.put("responseJson", response.optJSONArray("Data"));
                } else {
                    parseMap.put("responseJson", response.optJSONObject("Data"));
                }
            }


            JSONObject paramJson = (JSONObject) parseMap.get("paramJson");
            if (paramJson.opt("Data") != null) {
                if (paramJson.get("Data") instanceof JSONArray) {
                    parseMap.put("paramJson", paramJson.optJSONArray("Data"));
                } else {
                    parseMap.put("paramJson", paramJson.optJSONObject("Data"));
                }
            }


            String reqJsonCommentStr = "";
            for (int j = 0; j < paramInfo.length(); j++) {
                JSONObject reqParamObject = paramInfo.getJSONObject(j);
                String desc = reqParamObject.optString("desc");
                if (!StringUtils.isNotBlank(desc)) {
                    desc = reqParamObject.getString("name");
                }
                String type =  reqParamObject.optString("type");
                if (!StringUtils.isNotBlank(type)) {
                    type = "Object";
                }
                reqJsonCommentStr += reqParamObject.getString("name") + "\t";
                reqJsonCommentStr += desc + "\t";
                reqJsonCommentStr += type + "\t";
                reqJsonCommentStr += reqParamObject.optString("require") + "\t";
                reqJsonCommentStr += "" + "\t";
                reqJsonCommentStr += desc + "\n";

            }
            parseMap.put("reqJsonCommentStr", reqJsonCommentStr);

            String jsonCommentStr = "";
            for (int j = 0; j < responseParamInfo.length(); j++) {
                JSONObject responseParamObject = responseParamInfo.getJSONObject(j);
                String desc = responseParamObject.optString("desc");
                if (!StringUtils.isNotBlank(desc)) {
                    desc = responseParamObject.getString("name");
                }
                String type =  responseParamObject.optString("type");
                if (!StringUtils.isNotBlank(type)) {
                    type = "Object";
                }
                jsonCommentStr += responseParamObject.getString("name") + "\t";
                jsonCommentStr += desc + "\t";
                jsonCommentStr += type + "\t";
                jsonCommentStr += responseParamObject.optString("require") + "\t";
                jsonCommentStr += "" + "\t";
                jsonCommentStr += desc + "\n";

            }
            parseMap.put("jsonCommentStr", jsonCommentStr);

            checkSaveMarkdownToFile(parseMap);


            listData.add(parseMap);
        }

        System.out.println("paramKeyTypes===" + paramKeyTypes.toString());
        return listData;
    }

    private static int currentIndex = 0;

    private static boolean hasReqtJsonStr(String info) {
        if (info.substring(0, info.indexOf("{")).contains("响应参数")) {
            return false;
        }
        return true;
    }

    //number, boolean, string, integer, object, object [], string [],
    public static JSONObject convertListParamToJsonObj(boolean request, JSONArray paramInfo, Map<String, Object> parseMap) {

        String[] infos = parseMap.get("desc").toString().trim().split("\n");
        String info = infos[infos.length - 1];
        try {

            if (info.contains("{")) {
                String newStr = "";
                System.out.println(parseMap.get("url").toString());
                System.out.println(parseMap.get("desc").toString());
                for (int i = 0; i < infos.length - 1; i++) {
                    newStr += infos[i];
                }
                parseMap.put("desc", newStr);
                System.out.println(parseMap.get("url").toString());
                System.out.println(parseMap.get("methodType").toString());
                if (parseMap.get("url").toString().contains("cloud/carin")) {
                    if (request&&hasReqtJsonStr(info)) {
                        //"LaneName": "东门入口"
                        info = info.substring(info.indexOf("{")).replace("\"东门入口\"", "\"东门入口\",");
                    }
                }
                if (parseMap.get("url").toString().contains("cloud/carout")) {
                    if (request&&hasReqtJsonStr(info)) {
                        //"LaneName": "东门入口"
                        info = info.substring(info.indexOf("{")).replace("\"LaneID\":1，", "\"LaneID\":1,").replace("\"东门出口\"，", "\"东门出口\",");
                    }
                }
                if (parseMap.get("url").toString().contains("vms/Protocol")) {
                    if (request&&hasReqtJsonStr(info)&&parseMap.get("methodType").toString().equals("put")) {
                        //"LaneName": "东门入口"
                        info = info.substring(info.indexOf("{")).replace("“ID”：“1”", ",\"ID\":1,");
                    }
                }
                //“ID”：“1”
                if (parseMap.get("url").toString().contains("vms/LaneInfo")) {
                    if (!request) {

                        info = info.substring(info.indexOf("{")).replace(" \"BarrierStatus\":1", "");
                    }
                }


                if (!request) {
                    if (info.contains("响应参数")) {
                        info = info.substring(info.indexOf("响应参数") + 4);
                        JSONObject jsonObject = new JSONObject(info.substring(info.indexOf("{")));
                        return jsonObject;
                    }
                } else {
                    if (request && hasReqtJsonStr(info)) {
                        JSONObject jsonObject = new JSONObject(info.substring(info.indexOf("{")));
                        return jsonObject;
                    }
                }

            }

            return convertListParamToJsonObj(paramInfo);


        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    public static JSONObject convertListParamToJsonObj(JSONArray paramInfo) {
        JSONObject jsonObject = new JSONObject();
        currentIndex = 0;
        for (currentIndex = 0; currentIndex < paramInfo.length(); currentIndex++) {
            JSONObject paramObj = paramInfo.getJSONObject(currentIndex);
            parseParam(paramInfo, paramObj, jsonObject, 0);
        }
        return jsonObject;
    }

    private static JSONObject parseParam(JSONArray paramInfo, JSONObject paramObj, JSONObject jsonObject, int currentNode) {
        String name = paramObj.optString("name").trim();
        if (StringUtils.isNotBlank(name)) {
            String type = paramObj.optString("type");
            if (type.equals("number")) {
                jsonObject.put(name, 1.0);
            } else if (type.equals("integer")) {
                jsonObject.put(name, 1);
            } else if (type.equals("boolean")) {
                jsonObject.put(name, true);
            } else if (type.equals("string []") || type.equals("number []")) {
                JSONArray childObjArray = new JSONArray();
                childObjArray.put("String");
                jsonObject.put(name, childObjArray);
            } else if (type.equals("object")) {
                JSONObject childObj = new JSONObject();
                //treeNodeCheck
                currentIndex++;
                childObj = parseParam(paramInfo, paramInfo.getJSONObject(currentIndex), childObj, Integer.parseInt(paramObj.getString("treeNode")) + 1);
                jsonObject.put(name, childObj);
            } else if (type.equals("object []")) {
                JSONArray childObjArray = new JSONArray();
                //treeNodeCheck
                currentIndex++;
                for (int i = 0; i < 1; i++) {
                    JSONObject childObj = new JSONObject();
                    childObj = parseParam(paramInfo, paramInfo.getJSONObject(currentIndex), childObj, Integer.parseInt(paramObj.getString("treeNode")) + 1);
                    childObjArray.put(childObj);
                }
                jsonObject.put(name, childObjArray);
            } else if (type.equals("string")) {
                String defaultValue = paramObj.optString("defaultValue");
                if (StringUtils.isNotBlank(defaultValue)) {
                    jsonObject.put(name, defaultValue);
                } else {
                    jsonObject.put(name, "String");
                }

            }

            if (currentNode == Integer.parseInt(paramObj.getString("treeNode"))) {
                //in sub node
                if (paramInfo.length() > currentIndex + 1) {
                    JSONObject paramCheck = paramInfo.getJSONObject(currentIndex + 1);
                    if (currentNode == Integer.parseInt(paramCheck.getString("treeNode"))) {
                        //still the same
                        currentIndex++;
                        jsonObject = parseParam(paramInfo, paramCheck, jsonObject, Integer.parseInt(paramObj.getString("treeNode")));
                    }


                }

            }
        }
        return jsonObject;
    }

    static boolean saveMarkDownFile = false;

    private static void checkSaveMarkdownToFile(Map<String, Object> parseMap) throws Exception {
        if (saveMarkDownFile) {
            String title = (String) parseMap.get("title");
            String fileName = title.replaceAll("/", "-") + ".md";
            // 从JDK1.7开始提供的方法
            // 使用Files.write创建一个文件并写入
            try {
                Files.write(Paths.get(fileName), GenMarkDownUtils.generatorCode(parseMap).getBytes(StandardCharsets.UTF_8));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
    }

    public static String parseHtmlFromGreenCloudPath() {
        String html = "智慧停车系统场端平台API接口文档V2.1.html";
        String path = Paths.get("doc", html).toAbsolutePath().toString();
        return path;
    }

    public static String parseDocFromGreenCloudPath() {
        String html = "绿云会员接口手册.docx";
        String path = Paths.get("doc", html).toAbsolutePath().toString();
        return path;
    }

    public static String processParagraphs(XWPFParagraph paragraph, XWPFDocument doc) {

        List<XWPFRun> runs = paragraph.getRuns();
        String paragraphText = "";
        for (XWPFRun run : runs) {
            String text = run.getText(0);                                                                       //分段是poi来完成的,但是它分段的时候可能出现${CJ_CP}${CL_LX}这种情况
            System.out.println("text---->" + text);
            if (text != null) {
                paragraphText += text;
            }
        }
        System.out.println("@@@@=" + paragraphText);
        return paragraphText;
    }

    public static String readWord(String path) {
        String buffer = "";
        try {
            if (path.endsWith(".docx")) {
                InputStream is2 = new FileInputStream(new File(path));
                XWPFDocument docx = new XWPFDocument(is2);
                for (int i = 0; i < docx.getBodyElements().size(); i++) {
                    IBodyElement element = docx.getBodyElements().get(i);
                    if (element instanceof XWPFParagraph) {
                        XWPFParagraph xwpfParagraph = (XWPFParagraph) element;
                        processParagraphs(xwpfParagraph, docx);
                    } else {
                        //table.

                    }
                }
                int tables = docx.getTables().size();
                System.out.println(tables);
                docx.close();

                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();
                extractor.close();
            }
//            else if (path.endsWith("docx")) {
//                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
//                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
//                buffer = extractor.getText();
//                extractor.close();
//            }
            else {
                System.out.println("此文件不是word文件！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return buffer;
    }
}
